package com.xty.itgroup.tech05tytex.Mapper;

import com.xty.itgroup.tech05tytex.Databean.Bean03Contract;
import com.xty.itgroup.tech05tytex.Databean.Bean03ContractDetail;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface Mapper03Contract {
    @Insert("insert into tytex02contract_main(tex_contract_id,tex_contract_order_id,tex_contract_type,tex_contract_pos,tex_contract_time,tex_contract_buyer_id,tex_buyer_ticket,tex_contract_supplier_id,tex_supplier_ticket,tex_contract_clauses,is_lock,is_back,union_id,user_name,create_time)" +
            " values(#{tex_contract_id},#{tex_contract_order_id},#{tex_contract_type},#{tex_contract_pos}," +
            "#{tex_contract_time},#{tex_contract_buyer_id},#{tex_buyer_ticket},#{tex_contract_supplier_id}," +
            "#{tex_supplier_ticket},#{tex_contract_clauses},#{is_lock},#{is_back},#{union_id},#{user_name},Now()) ")
    @Options(useGeneratedKeys = true, keyProperty = "tex_id", keyColumn = "tex_id")
    public abstract void addContractMain(Bean03Contract bc);
    @Insert("<script>insert into tytex02contract_detail(main_id,order_num,detail_type,product_name,components,quantity,unit_price,gate_width,gram,extra_money,extra_remark,over_qr,delivery_date,remark,create_time) values" +
            "<foreach collection='details' item='detail' separator=','> " +
            "(" +
            "#{main_id},#{detail.order_num},#{detail.detail_type},#{detail.product_name},#{detail.components},#{detail.quantity},#{detail.unit_price},#{detail.gate_width},#{detail.gram},#{detail.extra_money},#{detail.extra_remark},#{detail.over_qr},#{detail.delivery_date},#{detail.remark},Now()" +
            ")" +
            "</foreach></script>")
    public abstract void addContractDetails(@Param("details") List<Bean03ContractDetail> details, int main_id);
    @Update("update tytex02contract_main set is_lock=2 where tex_id=#{tex_id}")
    public abstract void lockContract(int tex_id);
    @Select("SELECT tex_id,tex_contract_id,tex_contract_order_id,tex_contract_type,tex_contract_pos,tex_contract_time,tex_contract_buyer_id,tex_buyer_ticket,tex_contract_supplier_id,tex_supplier_ticket,tex_contract_clauses,is_lock,is_back,contract_back_date,union_id,user_name,create_time FROM tytex02contract_main where tex_id=#{tex_id}")
    public abstract Bean03Contract getContractMain(int tex_id);
    @Select("select contract_detail_id,main_id,order_num,detail_type,product_name,components,quantity,unit_price,gate_width,gram,extra_money,extra_remark,over_qr,delivery_date,remark,create_time from tytex02contract_detail where main_id=#{main_id}")
    public abstract List<Bean03ContractDetail> listContractDetail(int main_id);
    @Select("<script>SELECT a.tex_id,a.tex_contract_id,a.tex_contract_order_id,a.tex_contract_type,a.tex_contract_pos,DATE_FORMAT(a.tex_contract_time,'%Y-%m-%d %H:%i:%s') tex_contract_time,a.tex_contract_buyer_id,a.tex_contract_supplier_id,a.is_lock,a.is_back,DATE_FORMAT(a.contract_back_date,'%Y-%m-%d %H:%i:%s') contract_back_date,a.union_id,a.user_name,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') create_time,b1.customer_name buyer_name,b2.customer_name supplier_name " +
            "FROM tytex02contract_main a,tytex01customer b1,tytex01customer b2 where a.is_lock!=2 and a" +
            ".tex_contract_buyer_id=b1.id and a.tex_contract_supplier_id=b2.id " +
            "and a.user_name like #{user_name} and b1.customer_name like #{customer_name} and a.is_lock = #{status}" +
            "<when test='start_time!=null'> and Date(a.tex_contract_time) &lt;= Date(#{end_time}) and Date(a.tex_contract_time) &gt;= Date(#{start_time})  </when> " +
            "<when test='is_back != 0'>and a.is_back = #{is_back}</when>"+
            " ORDER BY a.is_lock desc,a.tex_contract_time desc </script>")
    public abstract List<Map<String, Object>> listContractMain(String user_name, String customer_name, String start_time, String end_time, int is_back, int status);

    //修改主表
    @Update("update tytex02contract_main set tex_contract_pos = #{tex_contract_pos}, tex_contract_time = #{tex_contract_time}, tex_contract_buyer_id = #{tex_contract_buyer_id}, " +
            "tex_buyer_ticket = #{tex_buyer_ticket},tex_contract_supplier_id = #{tex_contract_supplier_id}," +
            "tex_supplier_ticket = #{tex_supplier_ticket},tex_contract_clauses = #{tex_contract_clauses},is_lock = #{is_lock},is_back = #{is_back},union_id = " +
            "#{union_id}, user_name" +
            " = #{user_name},create_time = Now()" +
            "where tex_id = #{tex_id}")
    void updateContrat(Bean03Contract bc);
    //删除子表
    @Delete("delete from tytex02contract_detail where main_id = #{main_id}")
    void deleteDetailByMain(int main_id);
    @Update("update tytex02contract_main set is_lock=#{is_lock} where tex_id=#{tex_id}")
    public abstract void statusSet(int tex_id,int is_lock);
    @Update("update tytex02contract_main set is_back=#{is_back},contract_back_date=#{contract_back_date} where tex_id=#{tex_id}")
    public abstract void backDateSet(int tex_id, int is_back, String contract_back_date);
}
